package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.common.utils.VerifyCodeUtil;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.app.service.LoginService;
import com.atguigu.lease.web.app.service.SmsService;
import com.atguigu.lease.web.app.service.UserInfoService;
import com.atguigu.lease.web.app.vo.user.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private SmsService smsService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private UserInfoService userInfoService;


    @Override
    public void getCode(String phone) {
        String key = RedisConstant.APP_LOGIN_PREFIX + phone;
        //验证码必须6位
        String code = VerifyCodeUtil.getVerifyCode(6);
        //校验验证码是否过期
        if(redisTemplate.hasKey(key)){
            throw new LeaseException(ResultCodeEnum.APP_SEND_SMS_TOO_OFTEN);
        }
        //保存到redis中
        redisTemplate.opsForValue().set(key,code, 7 * 24 * 3600,TimeUnit.SECONDS);
        //发送验证码
        smsService.sendCode(phone,code);
    }

    @Override
    public String login(LoginVo loginVo) {

        //1.校验用户数据是否有效
        if(!StringUtils.hasLength(loginVo.getCode())){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
        }

        if(!StringUtils.hasLength(loginVo.getPhone())){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_PHONE_EMPTY);
        }


        //2.判断验证码是否存在
        String key = RedisConstant.APP_LOGIN_PREFIX + loginVo.getPhone();
        if(!redisTemplate.hasKey(key)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EXPIRED);
        }

        //3.校验验证码是否正确
        if(!redisTemplate.opsForValue().get(key).equals(loginVo.getCode())){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_ERROR);
        }

        //4.根据手机号查询userinfo表数据
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getPhone,loginVo.getPhone());
        UserInfo userInfo = userInfoService.getOne(queryWrapper);

        //5.判断是否存在
        if(userInfo == null){
            //第一次登录 则新增用户
            userInfo = new UserInfo();
            userInfo.setPhone(loginVo.getPhone());
            userInfo.setPassword(null);
            userInfo.setAvatarUrl("http://192.168.126.128:9000/test/title.jpg");
            userInfo.setNickname("尚庭用户:"+loginVo.getPhone());
            userInfo.setStatus(BaseStatus.ENABLE);
            userInfoService.save(userInfo);
        }

        //6.校验用户是否可用
        if(userInfo.getStatus() == BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.APP_ACCOUNT_DISABLED_ERROR);
        }

        //7.至此用户登录逻辑正确 生成JWT的token
        String token = JwtUtil.createToken(userInfo.getId());
        return token;
    }
}
